Page({
  data: {
    submitDispatch: false,
    id: null,
    singleDispatchTime: null,
    singleDispatchImgUrl: null,
    singleDispatchInfo: null,
    singleDispatchZone: null,
    bikeDispatchList: []
  },

  onLoad: function (event) {
    // 获得所有未处理的调度
    this.getUnhandledDispatches()
  },

  onShow: function (event) {
    this.getUnhandledDispatches()
  },

  goToBikeDispatch(event) {
    const state = event.currentTarget.dataset.state;
    if (state == '未处理') {
      this.setData({
        submitDispatch: true,
        singleDispatchTime: event.currentTarget.dataset.time,
        singleDispatchImgUrl: event.currentTarget.dataset.image,
        singleDispatchInfo: event.currentTarget.dataset.info,
        singleDispatchZone: event.currentTarget.dataset.zone,
        id: event.currentTarget.dataset.id
      })
    }
  },

  cancelSubmit() {
    this.setData({
      submitDispatch: false
    })
  },

  sendSubmit() {
    // 添加代码...
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要接受调度吗？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          wx.showLoading({
            title: '正在加载...'
          });
          wx.request({
            url: getApp().globalData.commonIP + '/bikeDispatch/acceptDispatchById',
            method: 'PUT',
            header: {
              'content-type': 'application/json', // 默认值
              'token': wx.getStorageSync('token')
            },
            data: {
              nickName: wx.getStorageSync('nickName'),
              id: that.data.id
            },
            success: res => {
              wx.hideLoading();
              if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: false,
                  complete: (res) => {
                    if (res.cancel) {

                    }

                    if (res.confirm) {
                      // 重置全局变量和去除缓存
                      getApp().globalData.wxusername = ''
                      getApp().globalData.wxuseravatar = ''
                      wx.removeStorageSync('nickName')
                      wx.removeStorageSync('avatarUrl')
                      wx.removeStorageSync('token')

                      wx.switchTab({
                        url: '/pages/index/index'
                      })
                    }
                  }
                })
                return
              }
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false
              })
              that.getUnhandledDispatches()
            }
          })
          // 关闭显示框
          that.setData({
            submitDispatch: false
          })
        }
      }
    })
  },

  rejectSubmit() {
    // 添加代码...
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要拒绝调度吗？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          wx.showLoading({
            title: '正在加载...'
          });
          wx.request({
            url: getApp().globalData.commonIP + '/bikeDispatch/rejectDispatchById',
            method: 'PUT',
            header: {
              'content-type': 'application/json', // 默认值
              'token': wx.getStorageSync('token')
            },
            data: {
              nickName: wx.getStorageSync('nickName'),
              id: that.data.id
            },
            success: res => {
              wx.hideLoading();
              if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: false,
                  complete: (res) => {
                    if (res.cancel) {

                    }

                    if (res.confirm) {
                      // 重置全局变量和去除缓存
                      getApp().globalData.wxusername = ''
                      getApp().globalData.wxuseravatar = ''
                      wx.removeStorageSync('nickName')
                      wx.removeStorageSync('avatarUrl')
                      wx.removeStorageSync('token')

                      wx.switchTab({
                        url: '/pages/index/index'
                      })
                    }
                  }
                })
                return
              }
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false
              })
              that.getUnhandledDispatches()
            }
          })
          // 关闭显示框
          that.setData({
            submitDispatch: false
          })
        }
      }
    })
  },

  getUnhandledDispatches() {
    var that = this
    wx.request({
      url: getApp().globalData.commonIP + '/bikeDispatch/getUnhandledBikeDispatches',
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              if (res.cancel) {

              }

              if (res.confirm) {
                // 重置全局变量和去除缓存
                getApp().globalData.wxusername = ''
                getApp().globalData.wxuseravatar = ''
                wx.removeStorageSync('nickName')
                wx.removeStorageSync('avatarUrl')
                wx.removeStorageSync('token')

                wx.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
          return
        }
        if (res.data.msg == '操作成功') {
          that.setData({
            bikeDispatchList: res.data.data
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  }

})